#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <asm/atomic.h>

#define     GPIOLED_CNT     1
#define     GPIOLED_NAME    "gpioled"
#define     LEDOFF          0
#define     LEDON           1

struct gpioled_dev
{
    dev_t devid;             /* 设备号   */
    int major;               /* 主设备号 */
    int minor;               /* 次设备号 */
    struct cdev cdev;        /* 字符设备 */
    struct class *class;     /* 类      */
    struct device_node *nd;  /* 设备节点 */
    struct device *device;   /* 设备     */
    int led_gpio;
    int dev_stats;           /* 设备状态， 0，设备未使用;>0,设备已经被使用 */
    spinlock_t lock;         /* 自旋锁 */
};

struct gpioled_dev gpioled;

static ssize_t gpioled_write(struct file *pfile, 
                            const char __user *buf, size_t cnt, loff_t *ppos){
    int ret = 0;
    unsigned char databuf[1];
    struct gpioled_dev *dev = pfile->private_data;
    ret = copy_from_user(databuf, buf, cnt);
    if(ret < 0) {
        printk("copy_from_user failed!\r\n");
        return -EINVAL;
    }
    if (databuf[0] == LEDON){
        gpio_set_value(dev->led_gpio, 0);
    }else if(databuf[0] == LEDOFF)
    { 
        gpio_set_value(dev->led_gpio, 1);
    }else
    {
        printk("command err! please input 0 or 1!\r\n");
        return -EINVAL;
    }

    return ret;

}

static int gpioled_open(struct inode *inode, struct file *pfile){
    unsigned long flags;
    pfile->private_data = &gpioled;

    spin_lock_irqsave(&gpioled.lock, flags);    /* 上锁 */
    if (gpioled.dev_stats) {                    /* 如果设备被使用了 */
        spin_unlock_irqrestore(&gpioled.lock, flags); /* 解锁 */
        return -EBUSY;
    }
    gpioled.dev_stats++;                        /* 如果设备没有打开，那么就标记已经打开了 */
    spin_unlock_irqrestore(&gpioled.lock, flags);   /* 解锁 */

    return 0;
    }

int gpioled_release(struct inode *pinode, struct file *pfile){
    unsigned long flags;
    struct gpioled_dev *dev = pfile->private_data;

    spin_lock_irqsave(&dev->lock, flags); /* 上锁 */
    if (dev->dev_stats) {
        dev->dev_stats--;
    }
    spin_unlock_irqrestore(&dev->lock, flags);/* 解锁 */

    return 0;
}

static const struct file_operations gpioled_fops= {
    .owner = THIS_MODULE,
    .write = gpioled_write,
    .open = gpioled_open,
    .release = gpioled_release,
};

static int __init gpioled_init(void){
    int ret = 0;
    spin_lock_init(&gpioled.lock);

    /* 1,申请设备号 */
    gpioled.major = 0;   /* 设备号由内核分配 */
    if(gpioled.major) {  /* 定义了设备号 */
        gpioled.devid = MKDEV(gpioled.major, 0);
        ret = register_chrdev_region(gpioled.devid, GPIOLED_CNT, GPIOLED_NAME);
    } else {            /* 没有给定设备号 */
        ret = alloc_chrdev_region(&gpioled.devid, 0, GPIOLED_CNT, GPIOLED_NAME);
        gpioled.major = MAJOR(gpioled.devid);
        gpioled.minor = MINOR(gpioled.devid);
    }   
    if(ret < 0) {
        goto fail_devid;
    }
    printk("gpioled_devid = %d\r\n", gpioled.devid);
    printk("gpioled_major = %d\r\n", gpioled.major);
    printk("gpioled_minor = %d\r\n", gpioled.minor);

    /* 2,添加字符设备 */
    gpioled.cdev.owner = THIS_MODULE;
    cdev_init(&gpioled.cdev, &gpioled_fops);
    ret = cdev_add(&gpioled.cdev, gpioled.devid, GPIOLED_CNT);
    if(ret < 0) 
        goto fail_cdev;
    printk("cdev_add successful!\r\n");

    /* 3,自动创建设备节点 */
    gpioled.class =  class_create(THIS_MODULE, GPIOLED_NAME);
    if (IS_ERR(gpioled.class)) {
        ret = PTR_ERR(gpioled.class);
		goto fail_class;
    }
    printk("class_create successful!\r\n");

    gpioled.device = device_create(gpioled.class, NULL, gpioled.devid, NULL, GPIOLED_NAME);
    if (IS_ERR(gpioled.device)) {
        ret = PTR_ERR(gpioled.device);
		goto fail_device;
    }
    printk("device_create successful!\r\n");

    /* 获取设备节点 */
    gpioled.nd = of_find_node_by_path("/gpioled");
    if (gpioled.nd == NULL) {    /* 失败 */
		ret = -EINVAL;
		goto fail_findnode;
	}
    printk("find dts node successful!\r\n");

    /* 2, 获取LED所对应的GPIO */
    gpioled.led_gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
    if(gpioled.led_gpio < 0) {
        printk("can't find led gpio\r\n");
        ret = -EINVAL;
        goto fail_findnode;
    }
    printk("led gpio num = %d\r\n", gpioled.led_gpio);
    
     /* 3,申请IO */
    ret = gpio_request(gpioled.led_gpio, "led-gpio");
	if (ret) {
		printk("Failed to request the led gpio\r\n");
		ret = -EINVAL;
        goto fail_findnode;
	}

    /* 4,使用IO，设置为输出 */
    ret = gpio_direction_output(gpioled.led_gpio, 1);
	if (ret) {
		goto fail_setoutput;
	}

    /* 5操作gpio*/
    gpio_set_value(gpioled.led_gpio, 0);

    return 0;


fail_setoutput:
    gpio_free(gpioled.led_gpio);
fail_findnode:
    device_destroy(gpioled.class, gpioled.devid);
fail_device:
    class_destroy(gpioled.class);
fail_class:
    cdev_del(&gpioled.cdev);
fail_cdev:
    unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
fail_devid:
    return ret;
}


static void __exit gpioled_exit(void){

    /* 关灯 */
    gpio_set_value(gpioled.led_gpio, 1);
    /* 释放IO */
    gpio_free(gpioled.led_gpio);

    /* 删除字符设备 */
    cdev_del(&gpioled.cdev);
    printk("cdev_del successful!\r\n");

    /* 释放设备号 */
    unregister_chrdev_region(gpioled.devid, GPIOLED_CNT);
    printk("unregister chrdev successful!\r\n");

    /* 摧毁设备*/
    device_destroy(gpioled.class, gpioled.devid);
    printk("device_destroy successful!\r\n");

    /* 摧毁类 */
    class_destroy(gpioled.class);
    printk("class_destroy successful!\r\n");
}

module_init(gpioled_init);
module_exit(gpioled_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhangkewei");
